<?php
/**
 * 增加考核计划,这个页面
 *
 * 10-新建计划
 * 15-主管驳回计划，
 * 20-提交待主管审核，
 * 30-主管审核通过(计划)，
 * 40-自评打分送审（待主管审核），
 * 35-主管驳回，
 * 48-主管审核待提交部门经理，
 * 50-部门经理待审核，
 * 45-部门经理驳回，
 * 55-HR驳回，
 * 60-HR待审核，
 * 70-HR通过
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Plan");
apf_require_class("Bll_Hr_Property");
class Plan_AddController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $date = $params['searchDate'];
        //check view user
        $this->user_id = $this->request->get_user_id();
        $this->plan_bll = new Bll_Jx_Plan();
        $this->view_user_info = $this->plan_bll->get_user_info_from_kq($this->user_id);
        $this->my_user_id = 5652;
        //取最新领导,直接从OA接中获取
        $this->super_info = array('name'=>'李不太白');
        //$this->view_user_info = array();
        $this->startMonth = intval(substr($date, 4,2));
        $this->endMonth = intval(substr($date, 6,2));
        $this->searchDate = $date;
        $this->year = date('Y',strtotime(substr($date, 0,6).'01'));
       # var_dump($this->startMonth);
#print_r($this->view_user_info);
        //确认user_plan自己的打分是整个量化的分还是单项
        //,是主管不打分，还是自己不打分就2分，打2分只是修改user_performance还是user_plan和user_plan_detail需要操作

        //考评开始时,如员工离职的不用同步过来
        //考评这段时间离职的,需要HR手动移出
        //如果一个员工免考了一次，在查看历史记录里这个月显示什么


        if($this->request->is_post_method()){
            tp($params);exit;
            $this->_build_params($params);
            $performance_id = $this->_post_performance($params);

            $this->_post_trial($performance_id, $params);

            $this->_post_kpi($performance_id, $params);

            $this->_post_review($performance_id, $params);

            $this->_post_culture($performance_id, $params);

            exit;
        }
        //$user_info = $this->plan_bll->get_user_info_from_kq(4555);
        //rint_r($user_info);


        //员工信息,是否试用期
        $is_trial = 0;
        if($this->view_user_info->indue_date !='0000-00-00 00:00:00' && time() < strtotime($this->view_user_info->indue_date) ){
            $is_trial =1;
        }
        $is_trial =1;
        //$this->user_id=4555;
        //$date = 20130403;

        $this->view_user_info->name='曹宏浪';
        $this->view_user_info->superior_code='SHF2938';
        $this->view_user_info->enter_date='2012-03-13 00:00:00';
        $this->view_user_info->department_id=15;
        $this->view_user_info->city_name = '上海';
        $this->view_user_info->group_name='总部';
        $this->view_user_info->department_name='技术部';
        $this->view_user_info->position_name='技术部111';

        $datalist = $this->plan_bll->get_plan_info($this->user_id,$date);
        if(empty($datalist['plan_info'])){//说明还没有填计划
            if(!empty($params)){//格式华为页面输出的,防止提交失败
                $datalist['plan_info'] = $this->_default_params_data($params);
            }
        }
        //tp($datalist);
        //根据当前的状态，分别控制自己打分，主管打分,主管评价
        $performance = empty($datalist['base_data']) ? array() : $datalist['base_data'];
        //$is_readonly = 0;
        //$this->request->set_attribute('is_readonly', $this->_operate_percent($performance));
        $this->request->set_attribute('is_readonly', 0);

        $this->request->set_attribute('review_type_list', Const_JxStatus::review_type_list());
        $this->request->set_attribute('datalist', $datalist);
        $this->request->set_attribute('is_trial', $is_trial);
        #$this->request->set_attribute('is_self', $my_uid);
        $this->request->set_attribute('year', $this->year);
        $this->request->set_attribute('super_info', $this->super_info);
        $this->request->set_attribute('m_to_m', $this->startMonth.'-'.$this->endMonth);
        $this->request->set_attribute('view_user_info', $this->view_user_info);
        return 'Plan_Add';
    }
    /**
     * 是否可以操作占比,三种状态可以编辑
     */
    private function _operate_percent($performance){
        $show = false;
        if(!empty($performance) && in_array($performance['status'],array(10,15,20))){
            $show = true;
        }
        return $show;
    }
    private function _build_params($params){
        $this->type_4 = 25;//企业文化固定
        //据量化kpi的值算出工作review的占比
        $this->type_2 = $params['kpiPercent'];
        $this->type_3 = 75-$this->type_2;
    }
    //默认$params
    private function _default_params_data($params){
        $data = array();
        if(!empty($params['name1'])){
            foreach ($params['name1'] as $k=> $val){
                $data[Const_JxStatus::PLAN_TYPE_1]['detail']=array(
                        'name'=>$val,
                        'performanceTarget'=>$params['performanceTarget1'][$k],
                        'optionPersent'=>0
                        );
            }
           // $data

        }
        if(!empty($params['name2'])){
            foreach ($params['name2'] as $k=> $val){
                $data[Const_JxStatus::PLAN_TYPE_2]['detail']=array(
                        'name'=>$val,
                        'performanceTarget'=>$params['performanceTarget2'][$k],
                        'optionPersent'=>$params['optionPersent2'][$k],
                );
            }
        }
        if(!empty($params['name3'])){
            foreach ($params['name3'] as $k=> $val){
                $data[Const_JxStatus::PLAN_TYPE_3]['detail']=array(
                        'name'=>$val,
                        'performanceTarget'=>$params['performanceTarget3'][$k],
                        'optionPersent'=>$params['optionPersent3'][$k],
                );
            }
        }
        if(!empty($params['name4'])){
            foreach ($params['name4'] as $k=> $val){
                $data[Const_JxStatus::PLAN_TYPE_4]['detail']=array(
                       // 实际表现和打分，主管打分，面谈
                        /*'name'=>$val,
                        'performanceTarget'=>$params['performanceTarget3'][$k],
                        'optionPersent'=>$params['optionPersent3'][$k],*/
                );
            }
        }
        return $data;
    }
    /**
     * 计划
     */
    private function _post_performance($params){
        $performance = $this->plan_bll->find_performance(array('userId'=>$this->user_id,'serachMonth'=>$this->searchDate));

        $user_info = $this->view_user_info;

        $submitType = $params['submitType']; //1为提交 2为保存
        if(empty($performance)){//在考评日开始之后,在制定计划截止日前入职的
            //有可能user_info_kp没有记录
            /*$kp = array(
                    'user_id'=>$this->user_id,
                    'name'=>$user_info->name,
                    'superior_code'=>$user_info->superior_code,
                    'enter_date'=>$user_info->enter_date,
                    'department_id'=>$user_info->department_id,
            );
            $this->plan_bll->update_user_kp($this->user_id,$kp);//有就更新,没有记录就新建
            */
            $city_bll = new Bll_Hr_Property();
            $city_list = $city_bll->get_sort_city_list();
            $cityId = array_search($user_info->city_name, $city_list);
            $p_data = array(
                    'userId'=>$this->user_id,
                    'year'=>date('Y'),
                    'startMonth'=>$this->startMonth,
                    'endMonth'=>$this->endMonth,
                    'serachMonth'=>$this->searchDate,
                    'businessPart'=>$user_info->group_name,//TO转换成数字........
                    'cityName'=>$user_info->city_name,
                    'cityId'=>$cityId,
                    'branchId'=>$user_info->department_id,
                    'branch'=>$user_info->department_name,
                    //'status'=>10,
                    'chk_leader_id'=>0 //主管确认时写入
            );
            $p_data['status'] = $submitType==1 ? Const_JxStatus::OPT_ACTION_20 : Const_JxStatus::OPT_ACTION_10;
            $performance_id = $this->plan_bll->insert_performance($p_data);
        }else{
            $performance_id = $performance[0]['id'];
            $status = $performance[0]['status'];
            if($performance[0]['status']==Const_JxStatus::OPT_ACTION_10){
                $status = $submitType==1 ? Const_JxStatus::OPT_ACTION_20 : Const_JxStatus::OPT_ACTION_10;
            }elseif($performance[0]['status']==Const_JxStatus::OPT_ACTION_30){//自己打分阶段
                $status = $submitType==1 ? Const_JxStatus::OPT_ACTION_40 : Const_JxStatus::OPT_ACTION_30;
            }
            //print_r($status);exit;
            if($performance['status'] != $status){//状态发生变化,update
                $this->plan_bll->update_performance_by_id($performance_id,array('status'=>$status));
            }
        }
        return $performance_id;
    }
    /**
     * 试用的员工
     */
    private function _post_trial($performance_id,$params){
        if(!empty($params['name1'])){
            $plan_detail = array();
            $plan = array(
                    'userId'=>$this->user_id,
                    'type'=>Const_JxStatus::PLAN_TYPE_1,
                    'percent'=>0,
                    'creatTime'=>time(),
                    'startMonth'=>$this->startMonth,
                    'endMonth'=>$this->endMonth,
                    'serachDate'=>$this->searchDate,
                    'user_performance_id'=>$performance_id
            );
            $parentId = $this->plan_bll->update_user_plan($plan);
            $this->plan_bll->remove_plan_detail($performance_id,Const_JxStatus::PLAN_TYPE_1);
            foreach ($params['name1'] as $k=>$val){
                if(!$val){//防止有空的
                    continue;
                }
                $plan_detail[] = array(
                        'parentId'=>$parentId,
                        'name'=>$val,
                        'optionPersent'=>'',
                        'performanceTarget'=>$params['performanceTarget1'][$k],//计划目标
                        'userId'=>$this->user_id,
                        'serachDate'=>$this->searchDate,
                        'type'=>Const_JxStatus::PLAN_TYPE_1,
                        'startMonth'=>$this->startMonth,
                        'endMonth'=>$this->endMonth,
                        'user_performance_id'=>$performance_id
                );
            }


            if($plan_detail){
                $this->plan_bll->batch_insert_detail($plan_detail);
            }
        }
        #tp($plan_detail);exit;
    }
    /**
     * 量化kpi
     */
    private function _post_kpi($performance_id,$params){
        if(!empty($params['name2'])){
            //删除原来的
            #$this->plan_bll->remove_plan($performance_id,Const_JxStatus::PLAN_TYPE_2);
            $plan_detail = array();
            $plan = array(
                    'userId'=>$this->user_id,
                    'type'=>Const_JxStatus::PLAN_TYPE_2,
                    'percent'=>$params['kpiPercent'],
                    'creatTime'=>time(),
                    'startMonth'=>$this->startMonth,
                    'endMonth'=>$this->endMonth,
                    'serachDate'=>$this->searchDate,
                    'user_performance_id'=>$performance_id
            );
            $parentId = $this->plan_bll->update_user_plan($plan);
            $this->plan_bll->remove_plan_detail($performance_id,Const_JxStatus::PLAN_TYPE_2);
            foreach ($params['name2'] as $k=>$val){
                if(!$val || !$params['optionPersent2'][$k]){//防止有空的
                    continue;
                }
                $plan_detail[] = array(
                        'parentId'=>$parentId,
                        'name'=>$val,
                        'optionPersent'=>$params['optionPersent2'][$k],
                        'performanceTarget'=>$params['performanceTarget2'][$k],//计划目标
                        'userId'=>$this->user_id,
                        'serachDate'=>$this->searchDate,
                        'type'=>Const_JxStatus::PLAN_TYPE_2,
                        'startMonth'=>$this->startMonth,
                        'endMonth'=>$this->endMonth,
                        'user_performance_id'=>$performance_id
                );
            }
            if($plan_detail){
                $this->plan_bll->batch_insert_detail($plan_detail);
            }
        }
        return true;
    }
    /**
     * 工作view处理
     */
    private function _post_review($performance_id,$params){
        if(!empty($params['name3'])){
            $review_detail = array();
            $review = array(
                    'userId'=>$this->user_id,
                    'type'=>Const_JxStatus::PLAN_TYPE_3,
                    'percent'=>$this->type_3,
                    'creatTime'=>time(),
                    'startMonth'=>$this->startMonth,
                    'endMonth'=>$this->endMonth,
                    'serachDate'=>$this->searchDate,
                    'user_performance_id'=>$performance_id
            );
            $parentId = $this->plan_bll->update_user_plan($review);
            //删除原来的
            $this->plan_bll->remove_plan_detail($performance_id,Const_JxStatus::PLAN_TYPE_3);
            foreach ($params['name3'] as $k=>$val){
                if(!$val || !$params['optionPersent2'][$k]){//防止有空的
                    continue;
                }
                $review_detail[] = array(
                        'parentId'=>$parentId,
                        'name'=>$val,
                        'optionPersent'=>$params['optionPersent3'][$k],
                        'performanceTarget'=>$params['performanceTarget3'][$k],//计划目标
                        'userId'=>$this->user_id,
                        'serachDate'=>$this->searchDate,
                        'type'=>Const_JxStatus::PLAN_TYPE_3,
                        'startMonth'=>$this->startMonth,
                        'endMonth'=>$this->endMonth,
                        'user_performance_id'=>$performance_id
                );
            }
            if($review_detail){
                $this->plan_bll->batch_insert_detail($review_detail);
            }
        }
        return true;
    }
    /**
     * 企业文化,第一次写入，之后就是update
     */
    private function _post_culture($performance_id,$params){
        $culture_list = Const_JxStatus::culture_list();
        $review_detail = array();
        $review = array(
                'userId'=>$this->user_id,
                'type'=>Const_JxStatus::PLAN_TYPE_4,
                'percent'=>$this->type_4,
                'creatTime'=>time(),
                'startMonth'=>$this->startMonth,
                'endMonth'=>$this->endMonth,
                'serachDate'=>$this->searchDate,
                'user_performance_id'=>$performance_id
        );
        $parentId = $this->plan_bll->update_user_plan($review);
        $this->plan_bll->remove_plan_detail($performance_id,Const_JxStatus::PLAN_TYPE_4);
        foreach($culture_list as $val){
            $review_detail[] = array(
                    'parentId'=>$parentId,
                    'name'=>$val['name'],
                    'optionPersent'=>$val['optionPersent'],
                    'performanceTarget'=>'',//计划目标
                    'userId'=>$this->user_id,
                    'serachDate'=>$this->searchDate,
                    'type'=>Const_JxStatus::PLAN_TYPE_4,
                    'startMonth'=>$this->startMonth,
                    'endMonth'=>$this->endMonth,
                    'user_performance_id'=>$performance_id
            );
        }
        if($review_detail){
            $this->plan_bll->batch_insert_detail($review_detail);
        }
    }
}